function getSystemAndArch() {
    const userAgent = navigator.userAgent;
    // 截取操作系统
    let system = "Unknown";
    if (/Windows/.test(userAgent)) {
        system = "Windows";
    } else if (/Macintosh/.test(userAgent)) {
        system = "MacOS";
    } else if (/Linux/.test(userAgent)) {
        system = "Linux";
    } else if (/Android/.test(userAgent)) {
        system = "Android";
    } else if (/iOS|iPhone|iPad|iPod/.test(userAgent)) {
        system = "iOS";
    }
    let arch = "Unknown";
    if (/x64|x86_64|Win64|WOW64/.test(userAgent)) {
        arch = "x64"; // 64 位架构
    } else if (/x86|i686|Win32/.test(userAgent)) {
        arch = "x86"; // 32 位架构
    } else if (/arm64|aarch64/.test(userAgent)) {
        arch = "arm64"; // ARM 64 位架构
    } else if (/arm/.test(userAgent)) {
        arch = "arm"; // ARM 32 位架构
    }
    return {
        os: system.toLocaleLowerCase(),
        arch: arch.toLocaleLowerCase(),
    };
}
const startRunningTime = Date.now();
function sendProductUsageInfo(a: any) {
    const { os, arch } = getSystemAndArch();
    const content = {
        version: a.version,
        commitdate: a.date,
        commitId: a.commitId,
        runningTime: ((Date.now() - startRunningTime) / 1000 / 60) | 0, // 产品运行时间(分钟)
        date: new Date().toLocaleString(), // 日期
        href: window.location.href, // 网址
        os: os, // 操作系统
        arch: arch, // 架构
    };
    const myHeaders = new Headers();
    const requestOptions: RequestInit = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
    };
    const c = window.encodeURIComponent(JSON.stringify(content));
    fetch(`https://account.bjxbsj.cn/api/product/record?product=EarthSDKJS&content=${c}`, requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
}
let copyright: {
    readonly owner: any;
    readonly ownerlink: any;
    readonly gituri: any;
    readonly info: string;
    readonly date: any;
    readonly author: any;
    readonly version: any;
    readonly name: any;
    readonly commitId: any;
    print(): void;
} | undefined = undefined;

try {
    // @ts-ignore
    const name = `${NAME_}` ?? '-';
    // @ts-ignore
    const version = VERSION_ ?? '-';
    // @ts-ignore
    const date = DATE_ ?? '-';
    // @ts-ignore
    const owner = OWNER_ ?? '-';
    // @ts-ignore
    const description = DESCRIPTION_ ?? '';
    // @ts-ignore
    const commitId = COMMITID_ ?? '';
    // @ts-ignore
    const timestamp = TIMESTAMP_ ?? '';
    const leftoverHoursStr = ((Date.now() - timestamp) / 3600000).toFixed(1);

    const info = `%c🌏 ${name}%c ${version}.${commitId.slice(0, 8)}.${date} (距今${leftoverHoursStr}个小时)
%c${description ? description + '\n' : ''}当前网站正在使用${name}，此软件版权归${owner}所有
`
    copyright = Object.freeze({
        // @ts-ignore
        get owner() { return OWNER_ },
        // @ts-ignore
        get ownerlink() { return OWNERLINK_ },
        // @ts-ignore
        get gituri() { return GITURI_ && GITURI_.replace('${commitId}', COMMITID_) },
        // @ts-ignore
        get info() { return info; },
        // @ts-ignore
        get date() { return DATE_; },
        // @ts-ignore
        get author() { return AUTHOR_; },
        // @ts-ignore
        get version() { return VERSION_; },
        // @ts-ignore
        get name() { return NAME_; },
        // @ts-ignore
        get commitId() { return COMMITID_; },
        print() {
            console.info(this.info, `
            font-size: 18px;
            font-weight: 1000;
            line-height: 1;
            color: rgb(0, 120, 215);
            padding-top: 20px;
            padding-bottom: 10px;
            `,
                `
            font-size: 10px;
            font-weight: 1000;
            color: rgb(0, 120, 215);
            `,
                `
            font-size: 10px;
            line-height: 1.5;
            color: rgb(0, 120, 215);
            padding-bottom: 2px;
            padding-left: 35px;
            `,
            );
        },
    });

    {
        // @ts-ignore
        window.g_XE3CopyRights = window.g_XE3CopyRights || {};
        // @ts-ignore
        window.g_XE3CopyRights[name] = copyright;
    }

    {
        // @ts-ignore
        window.g_XE3SendProductUsageInfo = window.g_XE3SendProductUsageInfo || true;
        // @ts-ignore
        if (window.g_XE3SendProductUsageInfo) {
            // 产品使用1分钟后发送使用情况
            setTimeout(() => sendProductUsageInfo(copyright), 1000 * 60);
            // 产品使用24小时后发送使用情况
            setInterval(() => sendProductUsageInfo(copyright), 24 * 60 * 60 * 1000);
        }
    }

    {
        //@ts-ignore
        window.g_earthsdk_copyright_print = window.g_earthsdk_copyright_print ?? true;
        //@ts-ignore
        window.g_earthsdk_copyright_print && copyright.print();
    }

} catch (error) {
    console.warn('版本信息有误！');
}

export { copyright };
